<?php
namespace jy_douyin;
defined('BY_JYA') or exit('error');
/*
	推送文章机制
*/
class set_data extends \plugin{
	public function upvideo($video){
		$setting=$this->plugin_setting('setting');
		$access_token=$setting['access_token'];
		$client_key=$setting['client_key'];
		if(!$access_token){
			exi('access_token未获取，需移步站点宣传--多媒体--抖音号--设置--授权获取access_token！','error');
		}
		if(!$client_key){
			exi('client_key未填写','error');
		}
		$url = 'https://open.douyin.com/video/upload?access_token='.$access_token.'&open_id='.$open_id;
		$post=array('video'=>$video);
		$post=ijson_encode($post);
		$res = cfc('http')->ihttp_request($url,$post);
		$content = @json_decode($content['content'], true);
		$content = $content['data'];
		if($content['error_code']) {
				$return['error']=1;
				$return['message']='抖音号授权失败, 请稍后重试！ 公众平台返回原始数据为: 错误代码-' . $content['error_code'] . '，错误信息-' . $content['description'];
		}
		return $content;
	}
	public function getAccessToken($code){//获取access_token
		$setting=$this->plugin_setting('setting');
		$return['error']=0;
		if(!$setting['access_token'] || $setting['expire']<TIMESTAMP){
			$url = "https://open.douyin.com/oauth/access_token?client_key=".$setting['client_key']."&client_secret=".$setting['client_secret']."&grant_type=authorization_code&code=".$code;
			$post=array('from'=>'jinyun');
			$post=ijson_encode($post);
			$content = cfc('http')->ihttp_request($url,$post);
			if(is_error($content)){
				$return['error']=1;
				$return['message']='抖音号授权失败, 请稍后重试！错误详情: ' . $content['message'];
			}
			$token = @json_decode($content['content'], true);
			$token = $token['data'];
			if($token['error_code']) {
				$return['error']=1;
				$return['message']='抖音号授权失败, 请稍后重试！ 公众平台返回原始数据为: 错误代码-' . $token['error_code'] . '，错误信息-' . $token['description'];
			}
			$setting['access_token'] = $token['access_token'];
			$setting['open_id'] = $token['open_id'];
			$setting['expire'] = TIMESTAMP + 28*24*3600;
			$this->plugin_setting('setting',$setting);
		}
		$return['access_token']=$setting['access_token'];
		return $return;
	}
}